<?php
    class Block_Register extends Zend_View_Helper_Abstract
    {
        public function register()
        {
            if($_POST['submit'])
            {
                $user = new Default_Model_User();
                $filter = new Zend_Filter_StringTrim();
                $fullname = $filter->filter($_POST['fullname']);
                $email = $filter->filter($_POST['email']);
                $password = $_POST['password'];
                $rePassword = $_POST['rePassword'];
                $gender = $_POST['gender'];
                $flag = true;
                            
                $validation = new Default_Form_ValidateUser();
                if(!$validation->validPassword($rePassword))
                {
                    $this->error = 'Mật khẩu từ 6-32 kí tự';
                    $flag = false;
                }
                if(!$validation->validPassword($password))
                {
                    $this->error = 'Mật khẩu từ 6-32 kí tự';
                    $flag = false;
                }
                if($validation->validEmail($email))
                {
                    $this->emailValue = $email;
                }
                else
                {
                    $this->error = 'Địa chỉ email không hợp lệ';
                    $flag = false;
                }
                if($user->checkUser($email) > 0)
                {
                    $thisw->error = 'Địa chỉ email này đã tồn tại';
                    $flag = false;
                }
                if($validation->validFullname($fullname))
                {
                    $this->fullnameValue = $fullname;
                }
                else
                {
                    $this->error = 'Tên đầy đủ không hợp lệ';
                    $flag = false;
                }
                
                if($flag == true)
                {
                    if($password == $rePassword)
                    {
                        $user->addUser($fullname, $email, $password, $gender);
                        $auth = Zend_Auth::getInstance();
                        
                        $authAdapter = new Zend_Auth_Adapter_DbTable($db);
                        $authAdapter->setTableName('user')
                                    ->setIdentityColumn('email')
                                    ->setCredentialColumn('password');
                                    
                        $authAdapter->setIdentity($email)
                                    ->setCredential(md5($password));
                                    
                        $select = $authAdapter->getDbSelect();
             			$select->where('level = 3 AND status = 1');
                        $result = $auth->authenticate($authAdapter);
                        
                        if($result->isValid())
                        {
                            $data = $authAdapter->getResultRowObject(null, array('password'));
                            $auth->getStorage()->write($data);
                        }
                        $this->_redirect('index/success');
                    }
                    else
                    {
                        $this->view->error = 'Mật khẩu không trùng khớp';
                    }
                }
            }
        }
    }
